﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using DefaultScope;
using Fabrikam.Phone.DataModel;
using Fabrikam.Phone.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;

namespace Fabrikam.Phone
{
    public partial class DetailsEdit : PhoneApplicationPage
    {
        public DetailsEdit()
        {
            InitializeComponent();
        }


        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (DataContext != null) return;

            string selectedIndex;

            if (!NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                ServiceTicketViewModel serviceTicketModel = new ServiceTicketViewModel();

                serviceTicketModel.ServiceTicket = new ServiceTickets();
                serviceTicketModel.ServiceTicket.ServiceTicketID = Guid.NewGuid();
                serviceTicketModel.ServiceTicket.EscalationLevel = 1;

                DataContext = serviceTicketModel;
                return;
            }

            Guid id = Guid.Parse(selectedIndex);

            ServiceTicketViewModel serviceTicketViewModel = App.ViewModel.ServiceTickets.FirstOrDefault(model => model.ServiceTicket.ServiceTicketID == id);

            if (serviceTicketViewModel == null)
                return;

            DataContext = serviceTicketViewModel;

            Customers customer = serviceTicketViewModel.Customer;

            if (customer == null)
                return;

            // because status will be changed, save value;
            var status = serviceTicketViewModel.StatusValue;

            Dispatcher.BeginInvoke(() => CustomerListPicker.SelectedItem = customer);
            Dispatcher.BeginInvoke(() =>
                StatusListPicker.SelectedItem = status);
        }


        private void ApplicationBarSaveClicked(object sender, EventArgs e)
        {
            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            serviceTicketViewModel.SaveCommand.Execute(null);

        }

        private void ApplicationBarDeleteClicked(object sender, EventArgs e)
        {

            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            serviceTicketViewModel.DeleteCommand.Execute(null);

        }

        private void CustomerListPicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            serviceTicketViewModel.Customer = (Customers)CustomerListPicker.SelectedItem;
        }

        private void StatusListPicker_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            ServiceTicketViewModel serviceTicketViewModel = DataContext as ServiceTicketViewModel;

            if (serviceTicketViewModel == null)
                return;

            serviceTicketViewModel.StatusValue = (int)StatusListPicker.SelectedItem;

        }
    }
}